package novel.spider.impl;

import novel.spider.NovelSiteEnum;
import novel.spider.entitys.Chapter;
import novel.spider.interfaces.IChapterSpider;
import novel.spider.util.NovelSpiderUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建一个类实现序列化文章接口
 * Created by Kau on 2016/10/30.
 */
public abstract class AbstractChapterSpider extends AbstractSpider implements IChapterSpider {



    public List<Chapter> getsChapter(String url) {
        try {
            String result = crawl(url);
            Document doc = Jsoup.parse(result);
            doc.setBaseUri(url);
            //通过配置文件获取文章选择器
            Elements as = doc.select(NovelSpiderUtil.getContext(NovelSiteEnum.GetEnumByUrl(url)).get("chapter-list-selector"));
            List<Chapter> chapters = new ArrayList<>();
            /**
             * 将获取到的标签和内容使用ArrayList存储
             */
            for (Element a : as){
                Chapter chapter = new Chapter();
                chapter.setTitle(a.text());
                //通过doc.setBaseUri过滤相对路径和绝对路径
                chapter.setUrl(a.absUrl("href"));
                chapters.add(chapter);
            }
            return chapters;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
